package leetcode.median;

/**
 * ClassName: $912_SortArray
 * Description:
 * date: 2022-02-13 19:46
 *
 * @author liyifan
 */
public class $912_SortArray {
    public int[] sortArray(int[] nums) {
        quickSort(nums, 0, nums.length - 1);
        return nums;
    }

    public void quickSort(int[] nums, int left, int right){
        if (left >= right)
            return;
        int mid = partition(nums, left, right);
        System.out.println("mid:" + mid + " left: " + left + " right:" + right);
        quickSort(nums, left, mid - 1);
        quickSort(nums, mid + 1, right);
    }

    private void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    private int partition(int[] nums, int left, int right){
        int lt = left;
        int pivot = nums[left];
        for (int i = left + 1;i <= right;i++) {
            if (nums[i] < pivot) {
                lt++;
                swap(nums, i, lt);
            }
        }
        swap(nums, left, lt);
        return lt;
    }
}
